<?php
class Reviews_Model extends Model {
	
	function __construct()
  	{
  		parent::Model();
  	}
  	
  	function insert($data="")
  	{
		if($data!="")
  				{
  					$result = $this->db->insert('reviews',$data);
  				}
  			return $result;
  	}
  	function update($id="",$data=array())
  	{
  		if($id !="" && !empty($data))
  		{
  			$this->db->where('id',$id); 
  			$res = $this->db->update('reviews',$data);
  		}
  		return $res;
  	}

  	
function Upload_Files($config = "", $field_name , $id = "",$data = array(),$is_thumb = FALSE)
	{
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload($field_name))
		{
			$error = array('error' => $this->upload->display_errors());
			$this->session->set_flashdata('message[]',$error);
			redirect($data['on_error_redirect']);
		}	
		else
		{
			$upload_data = array('upload_data' => $this->upload->data());
			
			if($data['rename_to']!="")
			{
				$filename = $config['upload_path'].$upload_data['upload_data']['file_name'];
				if(file_exists($filename))
				{
					$rename_to = $config['upload_path'].$data['rename_to'];
				if(rename($filename,$rename_to))
				{
					if($is_thumb == TRUE)
					{
						$config['image_library'] = 'gd2';
						$config['source_image'] = $rename_to;
						$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = TRUE;
						$config['quality'] = '100%';
						$config['width'] = 200;
						$config['height'] = 200;
						
						$this->load->library('image_lib', $config); 
						
						$this->image_lib->resize();
					}
					$adata = array(
               				$data['update_column'] => 0,
            		);
            				$where = array('id'	=> $id);
							$result = $this->db->update($data['update_table'], $adata,$where);
							if($result >=1)
							return 'true';
							else 
							{
							$this->delete_file($rename_to);
							return 'false';
							}
				}
				else 
				{
					$this->delete_file($filename);
					return 'false';
				}
				}
			}	

		}
	 
	}
  	function get_reviews($id="",$date_format=false,$start=0,$limit="",$count=false)
  	{ 
  		if($count == false)
  		{
		  		$query  = "SELECT ";
		  		if($id=="")
		  		{
		  		$query .= "movies.movie_name,movies.wallpapers_id,movies.interview_id,movies.gallery_id, reviews.id ,reviews.movie_id,reviews.music,reviews.direction ,reviews.producer , reviews.rating
		    	, reviews.image";
		
		  		if($date_format==false)
		  		$query.= ",reviews.insert_date ";
		  		else
		  		$query .= ",DATE_FORMAT(reviews.insert_date,'%D %b %y') as insert_date ";
		  		}
		  		 if($id!="")
		  		$query .="  reviews.movie_id
		    , reviews.id
		    , movies.movie_name,movies.wallpapers_id,movies.interview_id,movies.gallery_id
		    , reviews.insert_date
		    , reviews.punchline
		    , reviews.genre
		    , reviews.type
		    , reviews.banner
		    , reviews.cast
		    , reviews.art
		    , reviews.music
		    , reviews.cinematography
		    , reviews.stunts
		    , reviews.editing
		    , reviews.story
		    , reviews.screenplay
		    , reviews.direction
		    , reviews.producer
		    , reviews.summary
		    , reviews.description
		    , reviews.rating ";
		  		if($date_format==false)
		  		$query.= ",movies.release_date ";
		  		else
		  		$query .= ",DATE_FORMAT(movies.release_date,'%D %b %y') as release_date ";
		  		
		  		$query .="FROM 	reviews	INNER JOIN movies ON (reviews.movie_id = movies.id)
		    	where reviews.active = 0 and reviews.image = 0";
				
		  		if($id!="")
		  		$query .= " and reviews.id =".$id;
		  		
		  		$query .=" ORDER BY reviews.insert_date desc";
		  		if($id=="" && $limit!="")
		  		{
		  			$query .=" Limit $start,$limit";
		  		}
		  		
		  		$result =  $this->db->query($query);
		  		
		  		
		  		$res = $result;
		  		return $res;
  		}
  		else if($count == true)
  		{
  			$query = "SELECT COUNT(*) as count FROM reviews INNER JOIN movies ON (reviews.movie_id = movies.id)
		    		 where reviews.active = 0 and reviews.image = 0";
  			$result =  $this->db->query($query);
  			$count = $result->row();
  			return $count->count;
  		}
  	}
  	// function to get movie-id if reviews id is passed 
  	function get_movie_id($review_id="")
  	{
  		$result = $this->db->query('SELECT movie_id FROM  reviews where id = '.intval($review_id));
  		$res = $result->row();
  		return $res->movie_id;
  	}
  	function get_home_reviews()
  	{
  		$this->db->select('*');
  		$this->db->limit(6);
        //$this->db->from('reviews');
        $this->db->order_by('reviews.id','desc');
        $this->db->join('movies', 'movies.id = reviews.movie_id');
  		
  		
  		
  		$query = $this->db->get('reviews');
  		return $query->result();  		
  	}
  	
}
?>